package com.macbulldev.poc.testgin.controller;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.macbulldev.poc.testgin.domain.AppRole;
import com.macbulldev.poc.testgin.domain.AppUser;


@org.springframework.roo.addon.web.mvc.controller.RooWebScaffold(path = "approle", automaticallyMaintainView = true, formBackingObject = com.macbulldev.poc.testgin.domain.AppRole.class)
@org.springframework.web.bind.annotation.RequestMapping("/approle/**")
@org.springframework.stereotype.Controller
public class AppRoleController {
	private static final Logger LOGGER=Logger.getLogger(AppRoleController.class);

	@RequestMapping(value = "/approle", method = RequestMethod.POST)    
    public String create(@Valid AppRole appRole, BindingResult result, ModelMap modelMap) {    
        if (appRole == null) throw new IllegalArgumentException("A appRole is required");        
        if (result.hasErrors()) {        
            modelMap.addAttribute("appRole", appRole);            
            modelMap.addAttribute("appusers", AppUser.findAllAppUsers());            
            return "approle/create";            
        }        
        appRole.persist();        
        return "redirect:/approle/" + appRole.getId();        
    }

	@RequestMapping(value = "/approle/form", method = RequestMethod.GET)    
    public String createForm(ModelMap modelMap) {    
        modelMap.addAttribute("appRole", new AppRole());        
        modelMap.addAttribute("appusers", AppUser.findAllAppUsers());        
        return "approle/create";        
    }

	@RequestMapping(value = "/approle/{id}", method = RequestMethod.GET)    
    public String show(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("appRole", AppRole.findAppRole(id));        
        return "approle/show";        
    }

	@RequestMapping(value = "/approle", method = RequestMethod.GET)    
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, ModelMap modelMap) {    
       try
       {
		if (page != null || size != null) {        
            int sizeNo = size == null ? 10 : size.intValue();            
            modelMap.addAttribute("approles", AppRole.findAppRoleEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));            
            float nrOfPages = (float) AppRole.countAppRoles() / sizeNo;            
            modelMap.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));            
        } else {        
            modelMap.addAttribute("approles", AppRole.findAllAppRoles());            
        }
       }
       catch (Exception e)
       {
    	   LOGGER.error("Exception e",e);
       }
        return "approle/list";        
    }

	@RequestMapping(method = RequestMethod.PUT)    
    public String update(@Valid AppRole appRole, BindingResult result, ModelMap modelMap) {    
        if (appRole == null) throw new IllegalArgumentException("A appRole is required");        
        if (result.hasErrors()) {        
            modelMap.addAttribute("appRole", appRole);            
            modelMap.addAttribute("appusers", AppUser.findAllAppUsers());            
            return "approle/update";            
        }        
        appRole.merge();        
        return "redirect:/approle/" + appRole.getId();        
    }

	@RequestMapping(value = "/approle/{id}/form", method = RequestMethod.GET)    
    public String updateForm(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("appRole", AppRole.findAppRole(id));        
        modelMap.addAttribute("appusers", AppUser.findAllAppUsers());        
        return "approle/update";        
    }

	@RequestMapping(value = "/approle/{id}", method = RequestMethod.DELETE)    
    public String delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        AppRole.findAppRole(id).remove();        
        return "redirect:/approle?page=" + ((page == null) ? "1" : page.toString()) + "&size=" + ((size == null) ? "10" : size.toString());        
    }
}
